require 'FileUtils'

class InsdbController < ApplicationController

	
  def load_into_database
    dstr= String.new

    #sel_file = params[:file]['file']
    @errors = []
   if request.post?
     @sel_file=params[:file]['file']
     
     if @sel_file.empty?
       #redirect_to_index("Please select an input file")
       @errors = "Please select an input file"
       #redirect_to :action => :index,:controller=>'bifinfo'
     end 
      return if @sel_file.empty?
      IO.foreach(@sel_file) do |line| 
        arr = line.split("~") 
        break if arr[26] == nil
      # return if arr[25] == nil
        dstring=String.new
        dstring1=String.new
        dstring2=String.new
                
        dstring << arr[14]+" "+arr[26]+" "+arr[15]
  
        arr1 = arr[15].split("/")
         arr2 = arr[24].split("/")   
             
        dstring1 << arr1[2]+"-"+ arr1[1]+"-"+arr1[0]
        #vd = Date.new(arr1[2],arr1[1],arr1[0])

        dstring2 << arr2[2]+"-"+arr2[1]+"-"+arr2[0]
       
         infobif = Infobif.new(:module=> arr[0].strip,:batch_no=> arr[2].strip,:batch_id1=> "ibs",:interface=> "BI",:voucher_type=> "IB",:trans_type=> "GL",:client=> "EH",:account=> "B5401",:dim_1=> " ",:tax_code=> "0",:tax_system=> " ",:currency=> "GBP",:cur_amount=> arr[25].to_f,:amount=> arr[25].to_f, :description=> dstring, :trans_date=> dstring2, :voucher_date=> dstring1, :voucher_no=> 1, :period=> params[:arg1].to_i) 
  
        infobif.save
        dstr<< dstring +"\n"
        @s_module = arr[0]
        end
       end
        redirect_to :action => :index,:controller=>'bifinfo'
  
    #@kn_content = dstring1 
    #@kn_content_type='text/plain'
    #@response.headers["Content-Type"] = @kn_content_type
    #puts "successful successful"
     
  end
  
  def output_bif
      bifstr=String.new
      infobif1 = Infobif.new
      cur_at = 0
    Infobif.find(:all).each do |infobif| 
     cur_a = infobif.cur_amount * 100
     cur_at = cur_at + cur_a
     infobif1 = infobif
     end
 
    st_dt=String.new 
    t_dt =  String.new  
    t_dt << infobif1.trans_date.to_s

    if !t_dt.empty?

    at_dt = t_dt.split("-")
    st_dt << at_dt[0]+at_dt[1]+at_dt[2]

    else
    st_dt << " "
    end
    

    sv_dt=String.new 
    v_dt =  String.new  
    v_dt << infobif1.voucher_date.to_s
    if !v_dt.empty?
    av_dt = v_dt.split("-")
    sv_dt << av_dt[0]+av_dt[1]+av_dt[2]
    else
    sv_dt << " "
    end
    
      case infobif1.module.strip
        when "B" : 
              s_account = "R6101"
              s_dim1 = "3280"
        when "C" : 
              s_account = "B5903"
              s_dim1 = " "             
        when "S" : 
              s_account = "B6005"
              s_dim1 = " "             
        when "N" : 
              s_account = "B5904"
              s_dim1 = " "                   
        else 
              s_account = "B6005"
              s_dim1 = " "           
      end
      
    bifstr << infobif1.batch_id1.ljust(25," ")+infobif1.interface.ljust(25," ")+infobif1.voucher_type.ljust(25," ")+infobif1.trans_type.ljust(2," ")+infobif1.client.ljust(25," ")+s_account.ljust(25," ")+s_dim1.ljust(25," ")+" ".ljust(150," ")+infobif1.tax_code.ljust(25," ")+infobif1.tax_system.ljust(25," ")+infobif1.currency.ljust(25," ")+"  "+cur_at.to_i.to_s.rjust(20," ")+cur_at.to_i.to_s.rjust(20," ")+" ".ljust(71," ")+("Rent Allowances Batch Import "+infobif1.batch_no).ljust(255," ")+st_dt.ljust(8," ")+sv_dt.ljust(8," ")+infobif1.voucher_no.to_s.ljust(15," ")+infobif1.period.to_s.ljust(6," ")+" ".ljust(1277," ")+"\n"  

    Infobif.find(:all).each do |infobif| 
     cur_a = infobif.cur_amount * 100

    st_dt1=String.new 
    t_dt1 =  String.new      
    t_dt1 << infobif.trans_date.to_s
    if !t_dt1.empty? 
    at_dt1 = t_dt1.split("-")
    st_dt1 << at_dt1[0]+at_dt1[1]+at_dt1[2]
    else
    st_dt1 << " "
    end
   
    sv_dt1=String.new 
    v_dt1 =  String.new 
    v_dt1 << infobif.voucher_date.to_s
    if !v_dt1.empty?     
    av_dt1 = v_dt1.split("-")
    sv_dt1 << av_dt1[0]+av_dt1[1]+av_dt1[2]
    else
      sv_dt1 << " "  
    end
    bifstr << infobif.batch_id1.ljust(25," ")+infobif.interface.ljust(25," ")+infobif.voucher_type.ljust(25," ")+infobif.trans_type.ljust(2," ")+infobif.client.ljust(25," ")+infobif.account.ljust(25," ")+infobif.dim_1.ljust(25," ")+" ".ljust(150," ")+infobif.tax_code.ljust(25," ")+infobif.tax_system.ljust(25," ")+infobif.currency.ljust(25," ")+"  "+("-"+cur_a.to_i.to_s).rjust(20," ")+("-"+cur_a.to_i.to_s).rjust(20," ")+" ".ljust(71," ")+infobif.description.ljust(255," ")+st_dt1.ljust(8," ")+sv_dt1.ljust(8," ")+infobif.voucher_no.to_s.ljust(15," ")+infobif.period.to_s.ljust(6," ")+" ".ljust(1277," ")+"\n"  
     end
    
    File.open("C:/ibs.dat", "w")  { |f| f.write(bifstr) }

    @kn_content = bifstr 
    @kn_content_type='text/plain'
    @response.headers["Content-Type"] = @kn_content_type
    render :layout=>false
    # redirect_to :action => :index,:controller=>'bifinfo' 
  end 
     
end
